<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/09
 * Time: 15:28
 */
class AhpLoginsController extends BaseController{

    /*
   * @Author:      sqq
   * @DateTime:    2018-07-09 18:40:11
   * @Description: 验证token
   */
    public function checkToken(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');

        do{
            if($validator->fails()) {
                $response = Lang::get("wechat_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "checkToken", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /*
    * @Author:      sqq
   * @DateTime:    2018-07-09 18:43:37
   * @Description: ahp用户退出登录
   * */
    public function ahp_lgout(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $ahp_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ahp_user)){
                break;
            }
            try{
                AhpUserTokens::where('token',Input::get('token'))->delete();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "login", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("error.user_token_clean_error_2106");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "lgout", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


  /*
   * @Author:     sqq
   * @DateTime:    2018-07-09 13:44:32
   * @Description: ahp3.0用户登录
   * @link: http://www.activity.com/ahp/ahp3.0/ahp_login?user_name=shiqingqing&password=123456
   * */
    public function ahp_login(){
        $validator = Validator::make(Input::all(),
            array(
                'user_name' => 'required',
                'password' => 'required',
            )
        );
        do {
            if ($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = AhpUsers::getUser(Input::get('user_name'),Common::make(Input::get('password')));
            if(is_null($users)){
                $response = Lang::get("ahp_error.user_not_exit_2101");
                break;
            }
            if($users->status == 2){
                $response = Lang::get("ahp_error.user_frozen_error_2102");
                break;
            }
            if($users->status == 3){
                $response = Lang::get("ahp_error.user_deletet_error_2103");
                break;
            }

            //获取权限
            $ahp_user_resource = AhpRoleResource::getRoleResource($users->role_id);
            $menu = AhpRoleResource::getSystemMenu($users->role_id);

            try {
                AhpUserTokens::where('ahp_user_id', $users->id)->delete();
                $ahp_token = new AhpUserTokens();
                $ahp_token->ahp_user_id = $users->id;
                $ahp_token->token = md5(Input::get('user_name').time().rand(0,100));
                $ahp_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                $ahp_token->save();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "login", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.login_create_token_error_2105");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'id' => $users->id,
                    'token' => $ahp_token->token,
                    'out_time_at' => $ahp_token->out_time_at,
                    'user_name' => $users->user_name,
                    'real_name' => $users->real_name,
                    'desc' => $users->desc,
                    'status' => $users->status,
                    'updated_at' => $users->updated_at.'',
                    'created_at' => $users->created_at.'',
                    'user_resource' => $ahp_user_resource,
                    'user_menu' => $menu
                )
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}